﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DAL;
using BLL;
using NLog;
using Entities;
using DevExpress.XtraReports.UI;

namespace QLTTTH
{
    public partial class UCGiaoVien : DevExpress.XtraEditors.XtraUserControl
    {
        public UCGiaoVien()
        {
            InitializeComponent();
        }


        protected static Logger logger = LogManager.GetCurrentClassLogger();
        private void Log(ActionResult aResult)
        {
            foreach (var ex in aResult.Exceptions)
            {
                logger.Error(ex);
            }
        }


        private void UCGiaoVien_Load(object sender, EventArgs e)
        {
            Load_DSGiaoVien();
            grvGiaoVien.OptionsSelection.MultiSelect = true;
        }
        private void Load_DSGiaoVien()
        {
            var aResult = GiaoVienManager.LoadGiaoVien_List();
            if (aResult.Success)
                grDanhSach.DataSource = aResult.Result;
        }

        private void btnTimKiem_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            logger.Info("Thực hiện chức năng tìm kiếm");
            grvGiaoVien.ShowFindPanel();
        }

        private void btnHienThi_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            logger.Info("Thực hiện chức năng xem chi tiết thông tin giáo viên");
            frmThongTinGiaoVien frm = new frmThongTinGiaoVien();
            frm.HienThi_Control(LayGiaoVien_Listview());
            frm.ThietLapThongTin(LayNguoiDung_Listview());
            frm.btnLuu.Enabled = false;
            frm.btnXoaTrang.Enabled = false;
            frm.ShowDialog();
        }

        private GiaoVien LayGiaoVien_Listview()
        {
            var gv = new GiaoVien();
            gv.ChuyenMon = grvGiaoVien.GetFocusedRowCellValue("ChuyenMon").ToString();
            return gv;
        }

        private NguoiDung LayNguoiDung_Listview()
        {
            var ng = new NguoiDung();
            ng.HoTen = grvGiaoVien.GetFocusedRowCellValue("HoTen").ToString();
            ng.ChucVu = grvGiaoVien.GetFocusedRowCellValue("ChucVu").ToString();
            ng.MaNV = int.Parse(grvGiaoVien.GetFocusedRowCellValue("MaNV").ToString());
            ng.NgaySinh = DateTime.Parse(grvGiaoVien.GetFocusedRowCellValue("NgaySinh").ToString());
            ng.SoDT = grvGiaoVien.GetFocusedRowCellValue("SoDT").ToString();
            ng.DiaChi = grvGiaoVien.GetFocusedRowCellValue("DiaChi").ToString();
            ng.Email = grvGiaoVien.GetFocusedRowCellValue("Email").ToString();
            ng.TrangThai = grvGiaoVien.GetFocusedRowCellValue("TrangThai").ToString();
            ng.GioiTinh = grvGiaoVien.GetFocusedRowCellValue("GioiTinh").ToString();
            ng.MaNhomND = int.Parse(grvGiaoVien.GetFocusedRowCellValue("MaNhom").ToString());
            ng.Username = grvGiaoVien.GetFocusedRowCellValue("Username").ToString();
            ng.Password = grvGiaoVien.GetFocusedRowCellValue("Password").ToString();
            return ng;
        }

        private void btnThem_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            logger.Info("Chọn chức năng thêm giáo viên mới");
            frmThongTinGiaoVien frm = new frmThongTinGiaoVien();
            frm.sendData = new frmThongTinGiaoVien.SendData(ThemGiaoVien);
            frm.ShowDialog();
        }

        private void ThemGiaoVien(Entities.GiaoVienInfo gv)
        {
            var ngdung = new NguoiDung();
            ngdung.MaNhomND = gv.MaNhom;
            ngdung.HoTen = gv.HoTen;
            ngdung.GioiTinh = gv.GioiTinh;
            ngdung.Email = gv.Email;
            ngdung.DiaChi = gv.DiaChi;
            ngdung.ChucVu = gv.ChucVu;
            ngdung.Username = gv.Username;
            ngdung.Password = gv.Password;
            ngdung.NgaySinh = gv.NgaySinh;
            ngdung.TrangThai = gv.TrangThai;
            ngdung.SoDT = gv.SoDT;

            var vResult = new NhanVienManager().Validate(ngdung);
            if (vResult.Valid)
            {

                var manv = NhanVienManager.ThemNguoiDung(ngdung).Result;
                if (manv > 0)
                {
                    var gvien = new GiaoVien();
                    gvien.MaNguoiDung = manv;
                    gvien.ChuyenMon = gv.ChuyenMon;
                    var wResult = new GiaoVienManager().Validate(gvien);
                    if (wResult.Valid)
                    {
                        if (GiaoVienManager.ThemGiaoVien(gvien).Success)
                        {
                            XtraMessageBox.Show("Thêm giáo viên mới thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            Load_DSGiaoVien();
                        }
                        else
                        {
                            XtraMessageBox.Show("Thêm giáo viên mới không thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        }

                    }

                    else
                    {
                        logger.Info(wResult.ToErrorString());
                        XtraMessageBox.Show(wResult.ToString(), "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                }
            }
            else
            {
                logger.Info(vResult.ToErrorString());
                XtraMessageBox.Show(vResult.ToString(), "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
        }

        private void btnXoa_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            logger.Info("Chọn chức năng xóa giáo viên");
            if (XtraMessageBox.Show("Bạn có muốn xóa giáo viên này", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
            {
                int maGV;

                for (int i = 0; i < grvGiaoVien.RowCount; i++)
                {
                    if (grvGiaoVien.IsRowSelected(i))
                    {
                        maGV = int.Parse(grvGiaoVien.GetRowCellDisplayText(i, "MaNV").ToString());
                        GiaoVienManager.XoaGiaoVien(maGV);
                        NhanVienManager.XoaNhanVien(maGV);
                    }
                }

                grvGiaoVien.DeleteSelectedRows();

            }
        }

        private void btnCapNhat_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            logger.Info("Chọn chức năng cập nhật thông tin giáo viên");
            var frm = new frmThongTinGiaoVien();
            frm.HienThi_Control(LayGiaoVien_Listview());
            frm.ThietLapThongTin(LayNguoiDung_Listview());
            frm.txtChucVu.Enabled = false;
            frm.loupNhom.Enabled = false;
            frm.btnXoaTrang.Enabled = false;
            frm.sendData = new frmThongTinGiaoVien.SendData(CapNhatGiaoVien);
            frm.ShowDialog();
        }

        private void CapNhatGiaoVien(GiaoVienInfo gv)
        {
            var gvien = new GiaoVien();
            gvien.MaNguoiDung = gv.MaNV;
            gvien.ChuyenMon = gv.ChuyenMon;
            GiaoVienManager.CapNhatGiaoVien(gvien);
            grvGiaoVien.SetFocusedRowCellValue("MaNV", gvien.MaNguoiDung);
            grvGiaoVien.SetFocusedRowCellValue("ChuyenMon", gvien.ChuyenMon);
            var ngdung = new NguoiDung();
            ngdung.MaNV = gv.MaNV;
            ngdung.MaNhomND = gv.MaNhom;
            ngdung.HoTen = gv.HoTen;
            ngdung.GioiTinh = gv.GioiTinh;
            ngdung.Email = gv.Email;
            ngdung.DiaChi = gv.DiaChi;
            ngdung.ChucVu = gv.ChucVu;
            ngdung.Username = gv.Username;
            ngdung.Password = gv.Password;
            ngdung.NgaySinh = gv.NgaySinh;
            ngdung.TrangThai = gv.TrangThai;
            ngdung.SoDT = gv.SoDT;
            NhanVienManager.CapNhatNguoiDung(ngdung);
            grvGiaoVien.SetFocusedRowCellValue("MaNhomND", ngdung.MaNhomND);
            grvGiaoVien.SetFocusedRowCellValue("HoTen", ngdung.HoTen);
            grvGiaoVien.SetFocusedRowCellValue("GioiTinh", ngdung.GioiTinh);
            grvGiaoVien.SetFocusedRowCellValue("Email", ngdung.Email);
            grvGiaoVien.SetFocusedRowCellValue("DiaChi", ngdung.DiaChi);
            grvGiaoVien.SetFocusedRowCellValue("ChucVu", ngdung.ChucVu);
            grvGiaoVien.SetFocusedRowCellValue("Username", ngdung.Username);
            grvGiaoVien.SetFocusedRowCellValue("Password", ngdung.Password);
            grvGiaoVien.SetFocusedRowCellValue("NgaySinh", ngdung.NgaySinh);
            grvGiaoVien.SetFocusedRowCellValue("TrangThai", ngdung.TrangThai);
            grvGiaoVien.SetFocusedRowCellValue("SoDT", ngdung.SoDT);

        }

        private void btnIn_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            rptGiaoVien rpt = new rptGiaoVien();
            var gvien = GiaoVienManager.LoadGiaoVien_List().Result;
            rpt.DataSource = gvien;
            ReportPrintTool tool = new ReportPrintTool(rpt);
            tool.ShowPreview();
        }

    }
}
